Attribute VB_Name = "StringSplitExample4"
'@Folder "Examples.System.Strings.Methods"

'@Author Mark Johnstone
'@Project https://github.com/MarkJohnstoneGitHub/VBA-DotNetLib
'@Version v1.0 January 5, 2024
'@LastModified January 5, 2024

'@ReferenceAddin DotNetLib.tlb, mscorlib.tlb

'@Reference https://learn.microsoft.com/en-us/dotnet/api/system.string.split?view=netframework-4.8.1#system-string-split(system-char()-system-stringsplitoptions)

Option Explicit

''
' This example demonstrates the String.Split() methods that use
' the StringSplitOptions enumeration.
' The following example uses the StringSplitOptions enumeration to include or
' exclude substrings generated by the Split method.
''
Public Sub StringSplitExample4()
    Dim s1 As DotNetLib.String
    Set s1 = Strings.Create(",ONE,,TWO,,,THREE,,")
    Dim s2 As DotNetLib.String
    Set s2 = Strings.Create("[stop]" + _
                "ONE[stop][stop]" + _
                "TWO[stop][stop][stop]" + _
                "THREE[stop][stop]")
    
    Dim charSeparators As String
    charSeparators = ","
    Dim stringSeparators() As String
    Call VBArray.CreateInitialize1D(stringSeparators, "[stop]")
    Dim result() As String
    
    ' ------------------------------------------------------------------------------
    ' Split a string delimited by characters.
    ' ------------------------------------------------------------------------------
    Debug.Print VBString.Unescape("1) Split a string delimited by characters:\n")

    ' Display the original string and delimiter characters.
    Debug.Print VBString.Format("1a) The original string is ""{0}"".", s1)
    Debug.Print VBString.Format(VBString.Unescape("The delimiter character is '{0}'.\n"), charSeparators)

    ' Split a string delimited by characters and return all elements.
    Debug.Print "1b) Split a string delimited by characters and " + _
                  "return all elements:"
    result = s1.Split(charSeparators, StringSplitOptions.StringSplitOptions_None)
    Call Show(result)

    ' Split a string delimited by characters and return all non-empty elements.
    Debug.Print "1c) Split a string delimited by characters and " + _
                  "return all non-empty elements:"
    result = s1.Split(charSeparators, StringSplitOptions.StringSplitOptions_RemoveEmptyEntries)
    Call Show(result)

    ' Split the original string into the string and empty string before the
    ' delimiter and the remainder of the original string after the delimiter.
    Debug.Print "1d) Split a string delimited by characters and " + _
                  "return 2 elements:"
    result = s1.Split2(charSeparators, 2, StringSplitOptions.StringSplitOptions_None)
    Call Show(result)

    ' Split the original string into the string after the delimiter and the
    ' remainder of the original string after the delimiter.
    Debug.Print "1e) Split a string delimited by characters and " + _
                  "return 2 non-empty elements:"
    result = s1.Split2(charSeparators, 2, StringSplitOptions.StringSplitOptions_RemoveEmptyEntries)
    Call Show(result)

    ' ------------------------------------------------------------------------------
    ' Split a string delimited by another string.
    ' ------------------------------------------------------------------------------
    Debug.Print VBString.Unescape("2) Split a string delimited by another string:\n")
    
    ' Display the original string and delimiter string.
    Debug.Print VBString.Format("2a) The original string is ""{0}"".", s2)
    Debug.Print VBString.Format(VBString.Unescape("The delimiter string is ""{0}"".\n"), stringSeparators(0))

    ' Split a string delimited by another string and return all elements.
    Debug.Print "2b) Split a string delimited by another string and " + _
                      "return all elements:"
    result = s2.Split3(stringSeparators, StringSplitOptions.StringSplitOptions_None)
    Call Show(result)

    ' Split a string delimited by another string and return all elements.
    Debug.Print "2b) Split a string delimited by another string and " + _
                  "return all elements:"
    result = s2.Split3(stringSeparators, StringSplitOptions.StringSplitOptions_None)
    Call Show(result)

    ' Split the original string at the delimiter and return all non-empty elements.
    Debug.Print "2c) Split a string delimited by another string and " + _
                  "return all non-empty elements:"
    result = s2.Split3(stringSeparators, StringSplitOptions.StringSplitOptions_RemoveEmptyEntries)
    Call Show(result)

    ' Split the original string into the empty string before the
    ' delimiter and the remainder of the original string after the delimiter.
    Debug.Print "2d) Split a string delimited by another string and " + _
                  "return 2 elements:"
    result = s2.Split4(stringSeparators, 2, StringSplitOptions.StringSplitOptions_None)
    Call Show(result)

    ' Split the original string into the string after the delimiter and the
    ' remainder of the original string after the delimiter.
    Debug.Print "2e) Split a string delimited by another string and " + _
                  "return 2 non-empty elements:"
    result = s2.Split4(stringSeparators, 2, StringSplitOptions.StringSplitOptions_RemoveEmptyEntries)
    Call Show(result)
End Sub

' Display the array of separated strings using a local function
Private Sub Show(ByRef entries() As String)
    Debug.Print VBString.Format("The return value contains these {0} elements:", UBound(entries) + 1)
    Dim varEntry As Variant
    For Each varEntry In entries
        Debug.Print VBString.Format("<{0}>", varEntry);
    Next
    Debug.Print VBString.Unescape("\n\n")
End Sub

'/*
'This example produces the following results:
'
'1) Split a string delimited by characters:
'
'1a) The original string is ",ONE,,TWO,,,THREE,,".
'The delimiter character is ','.
'
'1b) Split a string delimited by characters and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'1c) Split a string delimited by characters and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'1d) Split a string delimited by characters and return 2 elements:
'The return value contains these 2 elements:
'<><ONE,,TWO,,,THREE,,>
'
'1e) Split a string delimited by characters and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO,,,THREE,,>
'
'2) Split a string delimited by another string:
'
'2a) The original string is "[stop]ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]".
'The delimiter string is "[stop]".
'
'2b) Split a string delimited by another string and return all elements:
'The return value contains these 9 elements:
'<><ONE><><TWO><><><THREE><><>
'
'2c) Split a string delimited by another string and return all non-empty elements:
'The return value contains these 3 elements:
'<ONE><TWO><THREE>
'
'2d) Split a string delimited by another string and return 2 elements:
'The return value contains these 2 elements:
'<><ONE[stop][stop]TWO[stop][stop][stop]THREE[stop][stop]>
'
'2e) Split a string delimited by another string and return 2 non-empty elements:
'The return value contains these 2 elements:
'<ONE><TWO[stop][stop][stop]THREE[stop][stop]>
'
'*/


